package com.hs.chd.action.search;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hs.chd.model.ChdPaperinfo;
import com.hs.chd.service.ISearchService;
import com.hs.core.action.BaseAction;
import com.hs.core.cache.DictCacheUtil;
import com.hs.core.db.DBMgr;
import com.hs.core.db.QueryPage;
@Controller  
public class SearchController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(SearchController.class);
	private static Map CONSTANTMAP = new HashMap();
	private ISearchService searchService;
	@Autowired  
	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	}
	 /**
	  * 文献检索列表形式
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/index.do")   
    public ModelAndView index(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         //DBMgr dbo = this.getDbo(request);
         //Map treedata = getTreeStr(dbo);
        // modelMap.addAllAttributes(treedata);
		 return new ModelAndView("app/search/abstractlist",modelMap);
		 //return new ModelAndView("app/search/searchIndex",modelMap);
		 
	 } 
	 /**
	  * 检索页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/datalistindex.do")   
    public ModelAndView datalistindex(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         //DBMgr dbo = this.getDbo(request);
         //Map treedata = getTreeStr(dbo);
        // modelMap.addAllAttributes(treedata);
		 return new ModelAndView("app/search/abstractlist",modelMap);
		 
	 } 
	 /**
	  * 文献检索摘要形式
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/dosearch.do")   
    public ModelAndView dosearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 //排序
 		 String searchtype = request.getParameter("searchtype");//简单检索，高级检索 ,中文期刊检索,英文期刊检索,古籍检索1,2,
 		 //String isdefault = request.getParameter("isdefault");
 		 if(StringUtils.isEmpty(searchtype)){
 			searchtype = "basic_search";
 		 }
 		 request.setAttribute("searchtype", searchtype);
 		 
// 		 if(null != isdefault){
// 			 modelMap.put("displayintro", "true");
// 			 return new ModelAndView("app/search/resultlist",modelMap);
// 		 }
 		 ModelAndView mv = new ModelAndView(); 
		 if("advsearch".equals(searchtype)){
			 mv = advSearch(request, response,modelMap);
		 }else if("advmod".equals(searchtype)){
			 mv = advModSearch(request, response,modelMap);
		 }else if("adveng".equals(searchtype)){
			 mv = advEngSearch(request, response,modelMap);
		 }else if("advbook".equals(searchtype)){
			 mv = advBookSearch(request, response,modelMap);
		 }else{
			mv = simpleSearch(request, response,modelMap);
		 } 
		 //String pstr = processParameter(request);
		// logger.debug("pstr:" + pstr);
		 //mv.getModelMap().put("paramstr", pstr);
		 return mv;
		 
	 } 
	 /**
	  * 接收表单提交的参数，构成字符串
	  * @param request
	  * @return
	  */
	 private static String processParameter(HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer();
		 Map parammap = request.getParameterMap();
		 if(parammap != null && !parammap.isEmpty()){
			 Set pset = parammap.entrySet();
			 Iterator iter = pset.iterator();
			 while(iter.hasNext()){
				 Entry entry = (Entry)iter.next();
				 String key = (String)entry.getKey();
				 String[] value = (String[])entry.getValue(); 
				// String value =  entry.getValue()+"";
				 buffer.append("&").append(key).append("=").append(value[0]);
			 }
		 }
		 return buffer.toString();
	 }
	 /**
	  * 简单检索
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  */
	 private ModelAndView simpleSearch(HttpServletRequest request,
		HttpServletResponse response,ModelMap modelMap){
		 String basiccol = request.getParameter("basiccol");//字段列名
		 String basicvalue = request.getParameter("basicvalue");//字段值
		 String basicspecial = request.getParameter("basicspecial");//查询方式：精确，模糊
 		 String orderName = request.getParameter("orderName");//排序字段
		 String orderNum = request.getParameter("orderNum");//排序方式
		 String onesql = request.getParameter("onesql");//一次检索sql 
		 String perPageCount = request.getParameter("perPageCount");//每页显示条数
		 String datarange = request.getParameter("datarange");//数据范围
		 String tags = request.getParameter("tags");//数据范围
		 String onekeyword = request.getParameter("onekeyword");//二次检索中第一次的检索词
		 String onesearchinfo = request.getParameter("onesearchinfo");//二次检索中第一次的检索词
		 String issyn = request.getParameter("issyn");//二次检索中第一次的检索词

  		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 //StringBuffer searchbuffer = new StringBuffer();
		 
		 
		  
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		  }else{
		    	orderByStr.append("a.ID").append(" desc");
		    	orderName = "a.ID";
		    	orderNum = "2";
		  }
		 StringBuffer allfullsql= new StringBuffer();
		 String keyword = null;
		 allfullsql.append("select a.* from CHD_PAPERINFO a where 1=1    ");
		 if(StringUtils.isNotEmpty(basicvalue)){
			 keyword = basicvalue;
			 if("0".equals(basicspecial)){
				 if("TITLE".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append(basiccol).append(")=lower('").append(basicvalue.trim()).append("')");
					 allfullsql.append(" or lower(a.TITLEN) =lower('").append(basicvalue.trim()).append("'))");
					 //searchbuffer.append("标题").append("“").append(basicvalue).append("”").append("&nbsp;");
				 }else if("PERIODICALNAME".equals(basiccol)||"PERIODICALNAMEENG".equals(basiccol)||"BOOKNAME".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("PERIODICALNAME").append(")=lower('").append(basicvalue.trim()).append("')");
					 allfullsql.append(" or lower(a.PERIODICALNAMEENG) =lower('").append(basicvalue.trim()).append("')");
					 allfullsql.append(" or lower(a.BOOKNAME) =lower('").append(basicvalue.trim()).append("'))");
					// searchbuffer.append("期刊名称").append("“").append(basicvalue).append("”").append("&nbsp;");
				 }else if("ABSTRACT".equals(basiccol)||"ABSTRACTENG".equals(basiccol)||"ORIGINALTEXT".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("ABSTRACT)").append("=lower('").append(basicvalue.trim()).append("')");
					 allfullsql.append(" or lower(a.ABSTRACTENG) =lower('").append(basicvalue.trim()).append("')");
					 allfullsql.append(" or lower(a.ORIGINALTEXT) =lower('").append(basicvalue.trim()).append("'))");
					 //searchbuffer.append("摘要/原文文本").append("“").append(basicvalue).append("”").append("&nbsp;");
				 }else if("SUBJECTWORD".equals(basiccol)||"SUBJECTWORDENG".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("SUBJECTWORD)").append("=lower('").append(basicvalue.trim()).append("')");
 					 allfullsql.append(" or lower(a.SUBJECTWORDENG) =lower('").append(basicvalue.trim()).append("'))");
					 //searchbuffer.append("主题词").append("“").append(basicvalue).append("”").append("&nbsp;");
				 }else if("KEYWORD".equals(basiccol)||"KEYWORDENG".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("KEYWORD)").append("=lower('").append(basicvalue.trim()).append("')");
 					 allfullsql.append(" or lower(a.KEYWORDENG) =lower('").append(basicvalue.trim()).append("'))");
					// searchbuffer.append("关键词").append("“").append(basicvalue).append("”").append("&nbsp;"); 
				 }else if("ALL".equals(basiccol)){//任意字段检索
					 allfullsql.append(allColumnsearch(basicvalue,basicspecial));
  					// searchbuffer.append("任意字段").append("“").append(basicvalue).append("”").append("&nbsp;"); 
				 }else if("DISNAME".equals(basiccol)){//疾病名称检索
					 if("1".equals(issyn)){
						// allfullsql.append(" and a.DATATYPE=1  and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
						// allfullsql.append(" SELECT id FROM CHD_TAGS WHERE TAGNAME IN( ");
						// allfullsql.append(" select B.OTHERNAME  from   CHD_STANDWORD B WHERE   B.RIGHTNAME = '"+basicvalue.trim()+"'");
						// allfullsql.append(" UNION all select C.RIGHTNAME  from   CHD_STANDWORD C WHERE   C.OTHERNAME ='"+basicvalue.trim()+"')))");
						 //allfullsql.append(" UNION all SELECT id from CHD_TAGS WHERE TAGNAME = '"+basicvalue.trim()+"'))");
						 String disnamestr = DictCacheUtil.getInst().getStdByname(basicvalue.trim());
						 allfullsql.append(" and a.DATATYPE=1  ");
						 if(!"".equals(disnamestr)){
							 allfullsql.append(" and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
							 allfullsql.append(" SELECT id FROM CHD_TAGS WHERE TAGNAME IN( ");
							 allfullsql.append(disnamestr);
							 allfullsql.append(")");
							 allfullsql.append(" UNION all SELECT id from CHD_TAGS WHERE TAGNAME in("+disnamestr+")");

							 allfullsql.append("))");
						 }else{
							 allfullsql.append(" and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
							 allfullsql.append("  SELECT id from CHD_TAGS WHERE TAGNAME = '"+basicvalue.trim()+"'");
							 allfullsql.append("))");
						 }
					 }else{
						 allfullsql.append(" and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
						 allfullsql.append(" SELECT id FROM CHD_TAGS WHERE  TAGNAME = '"+basicvalue.trim()+"'))");
					 }
 				 }else{
					  String colname = CONSTANTMAP.get(basiccol)+""; 
				      //searchbuffer.append(colname).append("“").append(basicvalue).append("”").append("&nbsp;"); 

					 allfullsql.append(" and lower(a.").append(basiccol).append(")=lower('").append(basicvalue.trim()).append("')");
				 }
				 
			 }else if("1".equals(basicspecial)){
				 if("TITLE".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append(basiccol).append(") like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
					 allfullsql.append(" or lower(a.TITLEN) like '%'||lower('").append(basicvalue.trim()).append("')||'%')");
					 //searchbuffer.append("标题").append("“").append(basicvalue).append("”").append("&nbsp;");

				 }else if("PERIODICALNAME".equals(basiccol)||"PERIODICALNAMEENG".equals(basiccol)||"BOOKNAME".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("PERIODICALNAME").append(") like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
					 allfullsql.append(" or lower(a.PERIODICALNAMEENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
					 allfullsql.append(" or lower(a.BOOKNAME) like '%'||lower('").append(basicvalue.trim()).append("')||'%')");
					 //searchbuffer.append("期刊名称").append("“").append(basicvalue).append("”").append("&nbsp;");

				 }else if("ABSTRACT".equals(basiccol)||"ABSTRACTENG".equals(basiccol)||"ORIGINALTEXT".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("ABSTRACT)").append(" like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
					 allfullsql.append(" or lower(a.ABSTRACTENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
					 allfullsql.append(" or lower(a.ORIGINALTEXT) like '%'||lower('").append(basicvalue.trim()).append("')||'%')");
					 //searchbuffer.append("摘要/原文文本").append("“").append(basicvalue).append("”").append("&nbsp;");

				 }else if("SUBJECTWORD".equals(basiccol)||"SUBJECTWORDENG".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("SUBJECTWORD)").append(" like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
 					 allfullsql.append(" or lower(a.SUBJECTWORDENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%')");
					 //searchbuffer.append("主题词").append("“").append(basicvalue).append("”").append("&nbsp;");

				 }else if("KEYWORD".equals(basiccol)||"KEYWORDENG".equals(basiccol)){
					 allfullsql.append(" and (lower(a.").append("KEYWORD)").append(" like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
 					 allfullsql.append(" or lower(a.KEYWORDENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%')");
					 //searchbuffer.append("关键词").append("“").append(basicvalue).append("”").append("&nbsp;"); 

				 }else if("DISNAME".equals(basiccol)){//疾病名称检索
					 if("1".equals(issyn)){
						 String disnamestr = DictCacheUtil.getInst().getStdByname(basicvalue.trim());
						 allfullsql.append(" and a.DATATYPE=1  ");
						 if(!"".equals(disnamestr)){
							 allfullsql.append(" and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
							 allfullsql.append(" SELECT id FROM CHD_TAGS WHERE TAGNAME IN( ");
							 allfullsql.append(disnamestr);
							 allfullsql.append(")");
							 allfullsql.append(" UNION all SELECT id from CHD_TAGS WHERE TAGNAME in("+disnamestr+")");

							 allfullsql.append("))");
							 
						 }else{
							 allfullsql.append(" and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
							  
							 allfullsql.append("  SELECT id from CHD_TAGS WHERE TAGNAME LIKE '%"+basicvalue.trim()+"%'");

							 allfullsql.append("))");
						 }
						 
//						 allfullsql.append(" select B.OTHERNAME  from   CHD_STANDWORD B WHERE   B.RIGHTNAME like '%"+basicvalue.trim()+"%'");
//						 allfullsql.append(" UNION all select C.RIGHTNAME  from   CHD_STANDWORD C WHERE   C.OTHERNAME like '%"+basicvalue.trim()+"%')))");
						 //allfullsql.append(" UNION all SELECT id from CHD_TAGS WHERE TAGNAME LIKE '%"+basicvalue.trim()+"%'))");
						 
					 }else{
						 allfullsql.append(" and a.id in (select PAPERID from CHD_PAPERTAGS where tagid in(");
						 allfullsql.append(" SELECT id FROM CHD_TAGS WHERE TAGNAME LIKE '%"+basicvalue.trim()+"%'))");
					 }
 				 }else if("ALL".equals(basiccol)){//任意字段检索
					 allfullsql.append(allColumnsearch(basicvalue,basicspecial));
  					 //searchbuffer.append("任意字段").append("“").append(basicvalue).append("”").append("&nbsp;"); 
				 }else{
					  String colname = CONSTANTMAP.get(basiccol)+""; 
				     // searchbuffer.append(colname).append("“").append(basicvalue).append("”").append("&nbsp;"); 

					 allfullsql.append(" and lower(a.").append(basiccol).append(") like '%'||lower('").append(basicvalue.trim()).append("')||'%' ");

				 }
			 }
		 } 
		 if(StringUtils.isEmpty( datarange)){
			 datarange = "0";
		 }
		 if(StringUtils.isNotEmpty(datarange)){
			 if(!"0".equals(datarange)){
				 allfullsql.append(" and a.DATATYPE=").append(datarange);
			 }
		 }
		 if(StringUtils.isNotEmpty(onesql)){
			 onesql = "select id from(" + onesql + ")";
			 allfullsql.append(" and a.id in (");
			 allfullsql.append(onesql);
			 allfullsql.append(")");
			 if(null!= keyword){
				 keyword += "|"+onekeyword;
			 }else{ 
			 	 keyword = onekeyword;
			 }
			// searchbuffer.append(" ").append(onesearchinfo);
		 }else{
			// onesearchinfo = searchbuffer.toString();
			 onekeyword = keyword;
		 }
		 System.out.println("onekeyword:" + onekeyword);
		 if(StringUtils.isNotEmpty(tags)){
			 allfullsql.append(" and a.id in( select PAPERID from CHD_PAPERTAGS where tagid in(");
			 allfullsql.append(tags).append("))"); 
		 }
		// allfullsql.append(orderByStr);
		 
		 String[] tagstr = genTagKeywords(request,tags);
		 if(!"".equals(tagstr[1])){
			 //searchbuffer.append("主题分类 ").append("“").append(tagstr[1]).append("“");
		 	 if(null!= keyword)
		 		keyword += "|"+tagstr[0];
		 	 else 
		 		keyword = tagstr[0];
		 	
		 }
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum); 
         conditionmap.put("basiccol", basiccol); 
         conditionmap.put("basicvalue", basicvalue); 
         conditionmap.put("basicspecial", basicspecial); 
         conditionmap.put("searchtype", "basic_search"); 
         conditionmap.put("perPageCount", perPageCount);
         conditionmap.put("datarange", datarange);
         conditionmap.put("onekeyword", onekeyword);
         
         conditionmap.put("tags", tags);
         //conditionmap.put("searchstrinfo", searchbuffer.toString());
         conditionmap.put("onesearchinfo", onesearchinfo);
         conditionmap.put("keyword", keyword);
         String sql = "select * from ("+allfullsql+")  where 1=1  ";
         if(StringUtils.isNotEmpty(datarange)&&!"0".equals(datarange)){
        	 sql = sql + " and datatype="+datarange;
         }
         logger.debug("sql:" + sql);
 		 QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/dosearch.do",sql,conditionmap); 
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum); 
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("onesql", allfullsql.toString()); 
	     modelMap.put("onekeyword", keyword); 
	     modelMap.put("searchtype", "basic_search"); 
	     modelMap.put("datarange", datarange);
	     modelMap.put("tags", tags);
	     //modelMap.put("searchstrinfo", searchbuffer.toString());
	     modelMap.put("onesearchinfo", onesearchinfo);
	     modelMap.put("keyword", keyword);
  		return new ModelAndView("app/search/resultlist",modelMap);
	 }
	 /**
	  * 根据tagid 获取tag名称
	  * @param request
	  * @param tags
	  * @return
	  */
	 private String[] genTagKeywords(HttpServletRequest request,String tags){
		 StringBuffer strbuffer = new StringBuffer();
		 StringBuffer strbuffer2 = new StringBuffer();
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 if(StringUtils.isNotEmpty(tags)){
			 List taglist = dbo.queryList("select tagname,tcmnamestr,(select name_en from CHD_SUBJECTCODE where name_cn=tagname) as enname from CHD_TAGS where id in("+tags+")", null);
			 for(int i = 0;i<taglist.size();i++){
				 Map tag = (Map)taglist.get(i);
				 String tname = (String)tag.get("TAGNAME");
				 String tcmname = (String)tag.get("TCMNAMESTR");
				 String enname = (String)tag.get("enname");
				 if(StringUtils.isNotEmpty(enname)){
					 strbuffer.append(enname).append("|");
					 strbuffer2.append(enname).append(" or ");
				 }
				 strbuffer.append(tname).append("|");
				 strbuffer2.append(tname).append(" or ");
				 if(StringUtils.isNotEmpty(tcmname)){
					 String[] tcmarr = tcmname.split(";");
					 for(int j = 0;j<tcmarr.length;j++){
						 strbuffer.append(tcmarr[j]).append("|");
						 strbuffer2.append(tcmarr[j]).append(" or ");
					 }
				 }
				 
			 }
		 }
		 return new String[]{strbuffer.toString(),strbuffer2.toString()};
	 }
	 /**
	  * 任意字段sql拼接
	  * @return
	  */
	 private static String allColumnsearch(String basicvalue ,String searchtype){
		 StringBuilder builder = new StringBuilder();
		 if("1".equals(searchtype)){
	 		 //标题、关键词、主题词、摘要/原文文本化、刊名、英文刊名、书籍名称、年份/朝代、期、基金、资助类别\作者、英文作者
			 builder.append(" and (lower(a.TITLE").append(") like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.TITLEN) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
	 		 builder.append(" or lower(a.").append("PERIODICALNAME").append(")like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.PERIODICALNAMEENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.BOOKNAME) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
	 		 builder.append(" or lower(a.").append("ABSTRACT)").append("like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.ABSTRACTENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.ORIGINALTEXT) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
	 		 builder.append(" or lower(a.").append("SUBJECTWORD)").append(" like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.SUBJECTWORDENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
	  		 builder.append(" or lower(a.").append("KEYWORD)").append(" like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.KEYWORDENG) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.AUTHOR) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.YEARSTR) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.PERIOD) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.BOOKCD) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.FUNDNAME) like '%'||lower('").append(basicvalue.trim()).append("')||'%'");
			 builder.append(" or lower(a.FUNDTYPE) like '%'||lower('").append(basicvalue.trim()).append("')||'%')"); 
		 }else if("0".equals(searchtype)){
	 		 //标题、关键词、主题词、摘要/原文文本化、刊名、英文刊名、书籍名称、年份/朝代、期、基金、资助类别\作者、英文作者
			 builder.append(" and (lower(a.TITLE").append(")=lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.TITLEN) =lower('").append(basicvalue.trim()).append("')");
	 		 builder.append(" or lower(a.").append("PERIODICALNAME").append(")=lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.PERIODICALNAMEENG) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.BOOKNAME) =lower('").append(basicvalue.trim()).append("')");
	 		 builder.append(" or lower(a.").append("ABSTRACT)").append("=lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.ABSTRACTENG) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.ORIGINALTEXT) =lower('").append(basicvalue.trim()).append("')");
	 		 builder.append(" or lower(a.").append("SUBJECTWORD)").append("=lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.SUBJECTWORDENG) =lower('").append(basicvalue.trim()).append("')");
	  		 builder.append(" or lower(a.").append("KEYWORD)").append("=lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.KEYWORDENG) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.AUTHOR) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.YEARSTR) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.PERIOD) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.BOOKCD) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.FUNDNAME) =lower('").append(basicvalue.trim()).append("')");
			 builder.append(" or lower(a.FUNDTYPE) =lower('").append(basicvalue.trim()).append("'))"); 
		 }
		 return builder.toString();
	 }
	 /**
	  * 高级检索
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  */
	 private ModelAndView advSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap){ 
 		 String orderName = request.getParameter("orderName");//排序字段
		 String orderNum = request.getParameter("orderNum");//排序方式
 		 String perPageCount = request.getParameter("perPageCount");//每页显示条数
		 String datarange = request.getParameter("datarange");//数据范围
		 String tags = request.getParameter("tags");//数据范围
		 String keywordstr = request.getParameter("keyword");//关键词
		 String searchstr = request.getParameter("searchstr");//检索式
		 
		 
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		  
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		  }else{
		    	orderByStr.append("id").append(" desc");
		    	orderName = "id";
		    	orderNum = "2";
		  }
		 StringBuffer allfullsql= new StringBuffer();
		 allfullsql.append("select a.* from CHD_PAPERINFO a where 1=1    ");
		 if(StringUtils.isEmpty( datarange)){
			 datarange = "0";
		 } 
		 if(StringUtils.isNotEmpty(datarange)){
			 if(!"0".equals(datarange)){
				 allfullsql.append(" and a.DATATYPE=").append(datarange);
			 }
		 }
		// StringBuffer searchstrinfo = new StringBuffer();
		 StringBuffer keywordbuffer = new StringBuffer();
		 //拼接sql
		 allfullsql.append(createSql(request,"txt_1",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_2",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_3",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_4",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_5",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_6",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_7",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_8",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_9",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_10",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_11",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_12",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_13",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_14",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_15",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_16",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_17",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_18",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_19",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_20",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_21",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_22",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_23",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_24",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_25",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_26",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_27",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_28",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_29",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_30",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_31",null,keywordbuffer));
		 allfullsql.append(createSql(request,"txt_32",null,keywordbuffer)); 
//		 allfullsql.append(createSql(request,"txt_28",searchstrinfo));
//		 allfullsql.append(createSql(request,"txt_29",searchstrinfo));
//		 allfullsql.append(createSql(request,"txt_30",searchstrinfo));
//		 allfullsql.append(createSql(request,"txt_31",searchstrinfo));
 		 
		 
		 if(StringUtils.isNotEmpty(tags)){
			 allfullsql.append(" and a.id in( select PAPERID from CHD_PAPERTAGS where tagid in(");
			 allfullsql.append(tags).append("))"); 
		 }
		 //allfullsql.append(orderByStr);
		 logger.debug("datarange:"+StringUtils.isEmpty( datarange));
		 String[] tagstr = genTagKeywords(request,tags);
//		 if(!"".equals(tagstr[1])){
//			 searchstrinfo.append("主题分类: ").append("“").append(tagstr[1]).append("“");
// 		 }
		 if(!"".equals(tagstr[0])){
 			 keywordbuffer.append(tagstr[0]);
		 }
		//翻页时使用从页面传过来的关键词
		 if("".equals(keywordbuffer.toString())&&StringUtils.isNotEmpty(keywordstr)){
			 keywordbuffer.append(keywordstr);
		 }
//		 if("".equals(searchstrinfo.toString()) && StringUtils.isNotEmpty(searchstr)){
//			 searchstrinfo.append(searchstr);
//		 }
		 String keyword = keywordbuffer.toString(); 
	     if(keyword.endsWith("|")){
	    	 keyword = keyword.substring(0,keyword.length()-1);
	     }
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum);  
         conditionmap.put("searchtype", "advsearch"); 
         conditionmap.put("perPageCount", perPageCount);
         conditionmap.put("datarange", datarange);
         conditionmap.put("tags", tags);
         //conditionmap.put("searchstr", searchstrinfo.toString());
         conditionmap.put("keyword", keyword);
         String sql = "select * from ("+allfullsql+")  where 1=1  ";
         if(StringUtils.isNotEmpty(datarange)&&!"0".equals(datarange)){
        	 sql = sql + " and datatype="+datarange;
         }
         logger.debug("sql:" + sql);
		 QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/dosearch.do",sql,conditionmap); 
//	     
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("onesql", allfullsql.toString()); 
	     modelMap.put("searchtype", "advsearch"); 
	     modelMap.put("datarange", datarange);		 
	     //modelMap.put("searchstrinfo", searchstrinfo.toString());
	     modelMap.put("tags", tags);	
	     modelMap.put("keyword", keyword);
		return new ModelAndView("app/search/resultlist",modelMap);
	 }
	 /**
	  * 高级检索现代中文期刊
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  */
	 private ModelAndView advModSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap){ 
 		 String orderName = request.getParameter("orderName");//排序字段
		 String orderNum = request.getParameter("orderNum");//排序方式
 		 String perPageCount = request.getParameter("perPageCount");//每页显示条数
		 String datarange = request.getParameter("datarange");//数据范围
		 String tags = request.getParameter("tags");//数据范围
		 String keywordstr = request.getParameter("keyword");
		 String searchstr = request.getParameter("searchstr");
		 
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		  
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		  }else{
		    	orderByStr.append("id").append(" desc");
		    	orderName = "id";
		    	orderNum = "2";
		  }
		 StringBuffer allfullsql= new StringBuffer();
		 allfullsql.append("select a.* from CHD_PAPERINFO a where 1=1   ");
		 StringBuffer searchstrinfo = new StringBuffer();
		 StringBuffer keywordbuffer = new StringBuffer();
		 //拼接sql
		 allfullsql.append(createSql(request,"mod_1",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_2",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_3",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_4",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_5",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_6",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_7",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_8",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_9",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_10",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_11",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_12",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_13",searchstrinfo,keywordbuffer));
		 allfullsql.append(createSql(request,"mod_14",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_15",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_16",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_17",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_18",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_19",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_20",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_21",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_22",searchstrinfo,keywordbuffer)); 
//		 allfullsql.append(createSql(request,"mod_23",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_24",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_25",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_26",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_27",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_28",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_29",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_30",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_31",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_32",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_33",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_34",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_35",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_36",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_37",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_38",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_39",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_40",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_41",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_42",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_43",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_44",searchstrinfo,keywordbuffer));
//		 allfullsql.append(createSql(request,"mod_45",searchstrinfo,keywordbuffer));
//		 
		 
		 
 		 
		 if(StringUtils.isNotEmpty(tags)){
			 allfullsql.append(" and a.id in( select PAPERID from CHD_PAPERTAGS where tagid in(");
			 allfullsql.append(tags).append("))"); 
		 }
		 String startyear = request.getParameter("startyearmod");
		 String endyear = request.getParameter("endyearmod");
		 if(StringUtils.isNotEmpty(startyear)&&StringUtils.isEmpty(endyear)){
			 allfullsql.append(" and length(YEARSTR)>=4 and substr(yearstr,0,4)>='");
			 allfullsql.append(startyear);
			 allfullsql.append("'");
		 }else if(StringUtils.isNotEmpty(startyear)&&StringUtils.isNotEmpty(endyear)){
			 allfullsql.append(" and length(YEARSTR)>=4 and substr(yearstr,0,4)>='");
			 allfullsql.append(startyear);
			 allfullsql.append("'");
			 allfullsql.append(" and substr(yearstr,0,4)<='");
			 allfullsql.append(endyear);
			 allfullsql.append("'");
		 }else if(StringUtils.isEmpty(startyear)&&StringUtils.isNotEmpty(endyear)){ 
			 allfullsql.append(" and length(YEARSTR)>=4 and substr(yearstr,0,4)<='");
			 allfullsql.append(endyear);
			 allfullsql.append("'");
		 }
		 
		 
		// allfullsql.append(orderByStr);
		 String[] tagstr = genTagKeywords(request,tags);
//		 if(!"".equals(tagstr[1])){
//			 searchstrinfo.append("主题分类: ").append("“").append(tagstr[1]).append("“");
// 		 }
		 if(!"".equals(tagstr[0])){
 			 keywordbuffer.append(tagstr[0]);
		 }
		//翻页时使用从页面传过来的关键词
		 if("".equals(keywordbuffer.toString())&&StringUtils.isNotEmpty(keywordstr)){
			 keywordbuffer.append(keywordstr);
		 }
//		 if("".equals(searchstrinfo.toString()) && StringUtils.isNotEmpty(searchstr)){
//			 searchstrinfo.append(searchstr);
//		 }
		 logger.debug("keywordstr:" + keywordstr);
		 String keyword = keywordbuffer.toString();
	     if(keyword.endsWith("|")){
	    	 keyword = keyword.substring(0,keyword.length()-1);
	     }
	      
	     
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum);  
         conditionmap.put("startyear", startyear);
         conditionmap.put("endyear", endyear); 
         conditionmap.put("searchtype", "advmod"); 
         conditionmap.put("perPageCount", perPageCount);
         conditionmap.put("datarange", datarange);
         conditionmap.put("tags", tags);
         conditionmap.put("keyword", keyword);
         conditionmap.put("searchstr", searchstrinfo.toString());
         
         String sql = "select * from ("+allfullsql+")  where datatype=1  ";
         
         System.out.println("sql:" + sql);
		 QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/dosearch.do",sql,conditionmap);
		 System.out.println("searchsql:" + qp.getSearchSql());

	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
 	     modelMap.put("searchtype", "advmod"); 
	     modelMap.put("datarange", datarange);	
	     modelMap.put("startyear", startyear);
	     modelMap.put("endyear", endyear); 
	     modelMap.put("searchstrinfo", searchstrinfo.toString());
	     modelMap.put("tags", tags);
	     modelMap.put("keyword", keyword);
		return new ModelAndView("app/search/resultlist",modelMap);
	 }
	 /**
	  * 高级检索现代英文期刊
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  */
	 private ModelAndView advEngSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap){ 
 		 String orderName = request.getParameter("orderName");//排序字段
		 String orderNum = request.getParameter("orderNum");//排序方式
 		 String perPageCount = request.getParameter("perPageCount");//每页显示条数
		 String datarange = request.getParameter("datarange");//数据范围
		 String tags = request.getParameter("tags");//数据范围
		 String keywordstr = request.getParameter("keyword");//关键词
		 String searchstr = request.getParameter("searchstr");//检索式
		 
		 
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		  
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		  }else{
		    	orderByStr.append("a.id").append(" desc");
		    	orderName = "a.id";
		    	orderNum = "2";
		  }
		// StringBuffer searchstrinfo = new StringBuffer();
		 StringBuffer keywordbuffer = new StringBuffer();
		 
		 StringBuffer allfullsql= new StringBuffer();
		 allfullsql.append("select a.* from CHD_PAPERINFO a where 1=1    ");
		 //拼接sql
		 allfullsql.append(createSql(request,"eng_1",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_2",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_3",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_4",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_5",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_6",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_7",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_8",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_9",null,keywordbuffer));
		 allfullsql.append(createSql(request,"eng_10",null,keywordbuffer));
//		 allfullsql.append(createSql(request,"eng_11",null,keywordbuffer));
//		 allfullsql.append(createSql(request,"eng_12",null,keywordbuffer));
//		 allfullsql.append(createSql(request,"eng_13",null,keywordbuffer));
//		 allfullsql.append(createSql(request,"eng_14",null,keywordbuffer));
//		 allfullsql.append(createSql(request,"eng_15"));
//		 allfullsql.append(createSql(request,"eng_16"));
//		 allfullsql.append(createSql(request,"eng_17"));
//		 allfullsql.append(createSql(request,"eng_18")); 
		//翻页时使用从页面传过来的关键词
		 if("".equals(keywordbuffer.toString())&&StringUtils.isNotEmpty(keywordstr)){
			 keywordbuffer.append(keywordstr);
		 }
		 String keyword = keywordbuffer.toString(); 
	     if(keyword.endsWith("|")){
	    	 keyword = keyword.substring(0,keyword.length()-1);
	     }
		 logger.debug("tags:" + tags);
		 if(StringUtils.isNotEmpty(tags)){
			 allfullsql.append(" and a.id in( select PAPERID from CHD_PAPERTAGS where tagid in(");
			 allfullsql.append(tags).append("))"); 
		 }
		 String startyear = request.getParameter("startyeareng");
		 String endyear = request.getParameter("endyeareng");
		 if(StringUtils.isNotEmpty(startyear)&&StringUtils.isEmpty(endyear)){
			 allfullsql.append(" and length(YEARSTR)>=4 and substr(yearstr,0,4)>='");
			 allfullsql.append(startyear);
			 allfullsql.append("'");
		 }else if(StringUtils.isNotEmpty(startyear)&&StringUtils.isNotEmpty(endyear)){
			 allfullsql.append(" and length(YEARSTR)>=4 and substr(yearstr,0,4)>='");
			 allfullsql.append(startyear);
			 allfullsql.append("'");
			 allfullsql.append(" and substr(yearstr,0,4)<='");
			 allfullsql.append(endyear);
			 allfullsql.append("'");
		 }else if(StringUtils.isEmpty(startyear)&&StringUtils.isNotEmpty(endyear)){ 
			 allfullsql.append(" and length(YEARSTR)>=4 and substr(yearstr,0,4)<='");
			 allfullsql.append(endyear);
			 allfullsql.append("'");
		 }
		// allfullsql.append(orderByStr);
		
		 logger.debug("allfullsql:" + allfullsql);
		 String[] tagstr = genTagKeywords(request,tags);
//		 if(!"".equals(tagstr[1])){
//			 searchstrinfo.append("主题分类: ").append("“").append(tagstr[1]).append("“");
// 		 }
		 if(!"".equals(tagstr[0])){
 			 keywordbuffer.append(tagstr[0]);
		 }
		
//		 if("".equals(searchstrinfo.toString()) && StringUtils.isNotEmpty(searchstr)){
//			 searchstrinfo.append(searchstr);
//		 }
		 
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum); 
         conditionmap.put("startyear", startyear);
         conditionmap.put("endyear", endyear); 
         conditionmap.put("searchtype", "adveng"); 
         conditionmap.put("perPageCount", perPageCount);
         conditionmap.put("datarange", datarange);
         conditionmap.put("tags", tags);
         conditionmap.put("keyword", keyword);
         //conditionmap.put("searchstr", searchstrinfo.toString());
         String sql = "select * from ("+allfullsql+")  where datatype=2  ";
		 QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/dosearch.do",sql,conditionmap); 
//	     if(null == perPageCount || "".equals(perPageCount)){
//			qp.setPerPageCount(10);
//		 }else {
//			qp.setPerPageCount(Integer.parseInt(perPageCount));
//		 }
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
 	     modelMap.put("startyear", startyear);
	     modelMap.put("endyear", endyear);
	     modelMap.put("searchtype", "adveng"); 
	     modelMap.put("datarange", datarange);	
	     //modelMap.put("searchstrinfo", searchstrinfo.toString());
	   
	     modelMap.put("keyword", keyword);
		return new ModelAndView("app/search/resultlist",modelMap);
	 }
	 /**
	  * 高级检索古籍
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  */
	 private ModelAndView advBookSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap){ 
 		 String orderName = request.getParameter("orderName");//排序字段
		 String orderNum = request.getParameter("orderNum");//排序方式
 		 String perPageCount = request.getParameter("perPageCount");//每页显示条数
		 String datarange = request.getParameter("datarange");//数据范围
		 
		 String tcmname = request.getParameter("tcmname");//中医病名
		 String tcmname_logic = request.getParameter("tcmname_logic");//排序方式
		 String tcmsyn = request.getParameter("tcmsyn");//排序方式
 		 String tags = request.getParameter("tags");//数据范围
 		 String keywordstr = request.getParameter("keyword");//关键词
		 String searchstr = request.getParameter("searchstr");//检索式
		 
		 
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		  
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		  }else{
		    	orderByStr.append("a.id").append(" desc");
		    	orderName = "a.id";
		    	orderNum = "2";
		  }
		 StringBuffer allfullsql= new StringBuffer();
		// StringBuffer searchstrinfo = new StringBuffer();
		 StringBuffer keywordbuffer = new StringBuffer();

		 
		 allfullsql.append("select a.* from CHD_PAPERINFO a where 1=1    ");
		 //拼接sql
		 allfullsql.append(createSql(request,"book_1",null,keywordbuffer));
		 allfullsql.append(createSql(request,"book_2",null,keywordbuffer));
		 allfullsql.append(createSql(request,"book_3",null,keywordbuffer));
		 allfullsql.append(createSql(request,"book_4",null,keywordbuffer));
		 allfullsql.append(createSql(request,"book_5",null,keywordbuffer)); 
		 allfullsql.append(createSql(request,"book_6",null,keywordbuffer)); 
		 allfullsql.append(createSql(request,"book_7",null,keywordbuffer)); 
 		 
		//翻页时使用从页面传过来的关键词
		 if("".equals(keywordbuffer.toString())&&StringUtils.isNotEmpty(keywordstr)){
			 keywordbuffer.append(keywordstr);
		 }
		 
		 logger.debug("tags:" + tags);
		 if(StringUtils.isNotEmpty(tags)){
			 allfullsql.append(" and a.id in( select PAPERID from CHD_PAPERTAGS where tagid in(");
			 allfullsql.append(tags).append("))"); 
		 }
		 if(StringUtils.isNotEmpty(tcmname)){
			 if("1".equals(tcmsyn)){
				 allfullsql.append(" and a.tcmname in(select OTHERNAME from CHD_STANDWORD where RIGHTNAME ='");
				 allfullsql.append(tcmname).append("'").append(" union all  select RIGHTNAME from CHD_STANDWORD where OTHERNAME ='");
				 allfullsql.append(tcmname).append("')"); 
			 }else{
				 if("0".equals(tcmname_logic)){
					 allfullsql.append(" and a.tcmname='").append(tcmname.trim()).append("'"); 
				 }
				 if("1".equals(tcmname_logic)){
					 allfullsql.append(" and a.tcmname like '&").append(tcmname.trim()).append("%'"); 
				 }
			 }
		 }
		// allfullsql.append(orderByStr);
		 String[] tagstr = genTagKeywords(request,tags);
//		 if(!"".equals(tagstr[1])){
//			 searchstrinfo.append("主题分类: ").append("“").append(tagstr[1]).append("“");
// 		 }
		 if(!"".equals(tagstr[0])){
 			 keywordbuffer.append(tagstr[0]);
		 }
		 String keyword = keywordbuffer.toString();
	     if(keyword.endsWith("|")){
	    	 keyword = keyword.substring(0,keyword.length()-1);
	     }
//	     if("".equals(searchstrinfo.toString()) && StringUtils.isNotEmpty(searchstr)){
//			 searchstrinfo.append(searchstr);
//		 }
		 logger.debug("allfullsql:" + allfullsql);
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum);  
         conditionmap.put("searchtype", "advbook"); 
         conditionmap.put("perPageCount", perPageCount);
         conditionmap.put("datarange", datarange);
         conditionmap.put("tcmname", tcmname);
         conditionmap.put("tcmname_logic", tcmname_logic);
         conditionmap.put("tcmsyn", tcmsyn);
         conditionmap.put("tags", tags);
         conditionmap.put("keyword", keyword);
         //conditionmap.put("searchstr", searchstrinfo.toString());
         String sql = "select * from ("+allfullsql+")  where datatype=3  ";
		 QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/dosearch.do",sql,conditionmap); 
//	     if(null == perPageCount || "".equals(perPageCount)){
//			qp.setPerPageCount(10);
//		 }else {
//			qp.setPerPageCount(Integer.parseInt(perPageCount));
//		 }
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
 	     modelMap.put("searchtype", "advbook"); 
	     modelMap.put("datarange", datarange);		
	     modelMap.put("tcmname", tcmname);
	     modelMap.put("tcmname_logic", tcmname_logic);
	     modelMap.put("tcmsyn", tcmsyn);
	     modelMap.put("tags", tags);
	     //modelMap.put("searchstrinfo", searchstrinfo.toString()); 
	     modelMap.put("keyword", keyword);
		return new ModelAndView("app/search/resultlist",modelMap);
	 }
	 
	 private String createSql(HttpServletRequest request,String cname,StringBuffer buffer,StringBuffer keywordbuffer){
		 StringBuffer sqlbuffer = new StringBuffer();
		 //值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 
		 String logicalname = "";
		 
		 String colname = (String)CONSTANTMAP.get(csel);
		 if(null != colname){
			 if("1".equals(clogical)){
				 logicalname = " OR ";
				 
			 }else{
				 logicalname = " AND ";
			 }
			// buffer.append(colname ).append("“").append(cvalue).append("”&nbsp;").append(logicalname).append("&nbsp;");
			 keywordbuffer.append(cvalue).append("|");
		 }
		 request.setAttribute(c_value, cvalue);
		 request.setAttribute(c_logical, c_logical);
		 request.setAttribute(c_sel, c_sel);
		 request.setAttribute(c_special, c_special);
		 //logger.debug("value:::" + clogical + "," + csel + "," + cvalue + "," + cspecial);
//		 if(StringUtils.isEmpty(cspecial))
//			 cspecial = "1";//默认为模糊检索
		 if(null == cvalue || StringUtils.isEmpty(cvalue)){//查询条件为空时返回空
			 return "";
		 }
		 if("0".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" and "); 
		 }else if("1".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" or "); 
		 }else{
			 sqlbuffer.append(" and "); 
		 }
		 sqlbuffer.append("lower(").append(csel).append(")");
		 if("0".equals(cspecial)){
			 //0 精确  1：模糊
			 sqlbuffer.append(" =lower('"); 
			 sqlbuffer.append(cvalue.trim());
			 sqlbuffer.append("')");
		 }
		 if("1".equals(cspecial)){
			//0 精确  1：模糊
			 sqlbuffer.append(" like '%'||lower('"); 
			 sqlbuffer.append(cvalue.trim());
			 sqlbuffer.append("')||'%'");
		 }
		 return sqlbuffer.toString();
	 }
	 /**
	  * 检索结果导出
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/export.do", method = RequestMethod.GET)   
     public ModelAndView dataListFrame(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		   	
		  return new ModelAndView("app/data/datalistframe",modelMap);
		 
	 }
	 /**
	  * 查看详细题录信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/viewdetail.do", method = RequestMethod.GET)   
     public ModelAndView viewdetail(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		  String pid = request.getParameter("pid");
		  if(StringUtils.isNotEmpty(pid)){ 
			  ChdPaperinfo obj = (ChdPaperinfo)this.searchService.findByID(ChdPaperinfo.class, new Long(pid));
			  modelMap.put("obj", obj);
		  }
		  return new ModelAndView("app/search/detail",modelMap);
		 
	 }
	 @RequestMapping("/search/tagstree.do")   
		public void listdicttree(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			 
			StringBuilder builder = new StringBuilder();
			builder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
			builder.append("<tree id=\"0\">\n");
			
			DBMgr dbutil = this.getDbo(request); 
			builder.append("<item text=\"").append("数据字典").append("\" open=\"1\" id=\"").append("10000").append("\">");
			builder.append("\n"); 
			List dataList = dbutil.queryList("select * from hs_code where dicttype=1 and youxbj=1 order by ordnum", null);
			for(int j=0;j<dataList.size();j++){
				Map node = (Map)dataList.get(j);
				String idNode = node.get("DICTCODE")+""; 
				builder.append("<item text=\"").append(node.get("DICTNAME")).append("\"");
				builder.append(" open=\"1\" id=\"").append(idNode).append("\" ");
				builder.append(">\n"); 
				builder.append("</item>\n");
			}
			builder.append("</item>\n"); 
			builder.append("</tree>"); 
			logger.info("builder：" + builder.toString());
			response.setCharacterEncoding("UTF-8");
	    	try {
				PrintWriter w = response.getWriter(); 
				 w.println(builder.toString());
				 w.flush();
				 w.close();
			} catch (IOException e) {
				 throw e;
			} 
		}
	 /***
	  * 生成左侧分类树
	  * @param dbo
	  * @return
	  */
	 private Map getTreeStr(DBMgr dbo){
		 Map hashMap = new HashMap();
		 StringBuffer tree1 = new StringBuffer("[");
		 StringBuffer tree2 = new StringBuffer("[");
		 StringBuffer tree3 = new StringBuffer("[");
		 StringBuffer tree4 = new StringBuffer("[");
		 String sql = "select * from CHD_TAGS where isdelete=1 order by isroot";
		 List nodelist = dbo.queryList(sql, null);
		 for(int i =0;i<nodelist.size();i++){
			 Map data = (Map)nodelist.get(i);
			 String id = data.get("ID") + "";
			 String pid = data.get("PID") + "";
			 String code = (String)data.get("TAGCODE");
			 String name = (String)data.get("TAGNAME");
			 String ttype  =  data.get("TAGTYPE")+"";
			 tree1.append("{id:").append(id).append(",pId:").append(pid).append(",");
			 tree1.append("name:\"").append(name).append("\",open:true},");
//			 //方剂分类
//			 if("1".equals(ttype)){
//				 tree1.append("{id:").append(id).append(",pId:").append(pid).append(",");
//				 tree1.append("name:\"").append(name).append("\",open:true},");
//			 //中药分类
//			 }else if("2".equals(ttype)){
//				 tree2.append("{id:").append(id).append(",pId:").append(pid).append(",");
//				 tree2.append("name:\"").append(name).append("\",open:true},");
//			 //证候分类
//			 }else if("3".equals(ttype)){
//				 tree3.append("{id:").append(id).append(",pId:").append(pid).append(",");
//				 tree3.append("name:\"").append(name).append("\",open:true},");
//			 //病名分类			
//			 }else if("4".equals(ttype)){
//				 tree4.append("{id:").append(id).append(",pId:").append(pid).append(",");
//				 tree4.append("name:\"").append(name).append("\",open:true},");
//			 } 
		 }
		 hashMap.put("treeStr1",cutStr(tree1.toString())+"]" );
		 hashMap.put("treeStr2",cutStr(tree2.toString())+"]" );
		 hashMap.put("treeStr3",cutStr(tree3.toString())+"]" );
		 hashMap.put("treeStr4",cutStr(tree4.toString())+"]");
		 return hashMap;
	 }
	 private static String cutStr(String str){
		 if(str.lastIndexOf(',') != -1){
			 str = str.substring(0, str.length()-1);
		 }
		 return str;
	 }
	 
	 static {
		 CONSTANTMAP.put("TITLE","标题");
		 CONSTANTMAP.put("TITLEN","标题");
		 CONSTANTMAP.put("PERIODICALNAME","刊名");
		 CONSTANTMAP.put("PERIODICALNAMEENG","刊名");
		 CONSTANTMAP.put("BOOKNAME","书籍名称");
		 CONSTANTMAP.put("AUTHOR","作者");
		 CONSTANTMAP.put("AUTHOREN","作者");
		 CONSTANTMAP.put("AUTHORUNIT","作者单位");
		 CONSTANTMAP.put("AUTHORADDRESS","作者通讯地址");
		 CONSTANTMAP.put("KEYWORD","关键词");
		 CONSTANTMAP.put("KEYWORDENG","关键词");
		 CONSTANTMAP.put("SUBJECTWORD","主题词");
		 CONSTANTMAP.put("SUBJECTWORDENG","主题词");
		 CONSTANTMAP.put("ABSTRACT","摘要");
		 CONSTANTMAP.put("ABSTRACTENG","摘要");
		 CONSTANTMAP.put("ORIGINALTEXT","原文文本");
		 CONSTANTMAP.put("LANGUAGE","语种");
		 CONSTANTMAP.put("DOCUMENTTYPE","文献类型");
		 CONSTANTMAP.put("CLASSNO","分类号");
		 CONSTANTMAP.put("FUNDSTR","资助情况");
		 CONSTANTMAP.put("YEARSTR","年份");
		 CONSTANTMAP.put("VOLUME","卷");
		 CONSTANTMAP.put("PERIOD","期");
		 CONSTANTMAP.put("PAGENO","页码");
		 CONSTANTMAP.put("VOLUMESECTION","卷册章节");
		 CONSTANTMAP.put("FUNDTYPE","资助类别");
		 CONSTANTMAP.put("COMFROM","文献来源");
		 CONSTANTMAP.put("AREASTR","国家\\省市");
		 CONSTANTMAP.put("CWNUM","参文数");
		 CONSTANTMAP.put("ISSNSTR","ISSN编号");
		 CONSTANTMAP.put("CONTRYCODE","国内代码");
		 CONSTANTMAP.put("CODESTR","内部代码");
		 CONSTANTMAP.put("PUBLISHAREA","出版地");
		 CONSTANTMAP.put("IMPRESSWORD","特征词");
		 CONSTANTMAP.put("UPDATEDATE","更新日期");
		 CONSTANTMAP.put("AUTHORABSTRACT","著者文摘");
		 CONSTANTMAP.put("FUNDNAME","基金");
		 CONSTANTMAP.put("CASTR","CA");
		 CONSTANTMAP.put("USERSUBJECT","人名主题");
		 CONSTANTMAP.put("TCMNAME","中医病名");
		 CONSTANTMAP.put("BOOKVERSION","书籍版本");
		 CONSTANTMAP.put("INPUTUSER","录入人"); 
		 CONSTANTMAP.put("BOOKCD","朝代"); 
	 }
	 
}
